package com.py.credit.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.dao.ImmediatePerDao;
import com.py.credit.service.ImmediatePerService;
import com.py.framework.core.helper.UrlHelper;
import com.py.framework.core.http.HttpAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ImmediatePerServiceImpl implements ImmediatePerService {

    @Autowired
    private ImmediatePerDao immediatePerDao;
    @Autowired
    private UrlHelper urlHelper;

    @Override
    public List<Map<String, Object>> getInfoByFrXk() {

        return immediatePerDao.getInfoByFrXk();
    }

    @Override
    public List<Map<String, Object>> getInfoByFrCf() {
        return immediatePerDao.getInfoByFrCf();
    }

    @Override
    public List<Map<String, Object>> getInfoByZrrXk() {
        return immediatePerDao.getInfoByZrrXk();
    }

    @Override
    public List<Map<String, Object>> getInfoByZrrCf() {
        return immediatePerDao.getInfoByZrrCf();
    }

    @Override
    public void updateTableImmePer(List<Map<String, Object>> list, String table) {
        if (!list.isEmpty()) {
            immediatePerDao.updateTableImmePer(list, table);
        }
    }

    @Override
    public RestServiceResult<List<Map<String, Object>>> getDateInfo() {
        String url = urlHelper.getBaseServerUrl() + "/holiday/findAllFar.json";
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpGet(url, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<Map<String, Object>>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<Map<String, Object>>> areaData = new RestServiceResult<>();
            areaData.setCode(RestServiceResult.CODE_UNKNOW);
            areaData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-获取工作日时间数据调用异常！(ImmediatePerService#getDateInfo) URL:" + url + " Params:"
                    + "Result:" + result, e.getMessage(), e);
            return areaData;
        }
    }

}
